home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-03-02 | 13.5 KB | 472 lines | [TEXT/CWIE] |
- //handle customer window stuff.
- #include "InvoiceGlobals.h"
- #include "BTreeDef.h"
- #include "BTreeProtos.h"
- #include "SampleHeader.h"
- #include "Actions.h"
-
- //some vars local to this file
- static short itemType;
- static Handle item;
- static Rect box;
- static Str255 itemString;
- static short ScratchShort;
- static long ScratchLong;
- //FindType = 1 means by num, 2 by name
- // these variables are used in searches
- Str255 SearchNameP;
- Str255 FoundNameP;
- long SearchNumP;
- long FoundNumP;
- FileAddr CurrentCustomerAddr;
- short gCustomerFindType = kFindByName;
-
- #define kPopUp 6
- #define kDoIt 1
-
- //three utility functions; makes life much easier
-
- void GetFromCustDialog(void);
- void PutToCustDialog(void);
- short RetrieveString (void);
-
-
- //the heart of the matter
-
- short FindCustByNumber(void);
- short FindCustByName(void);
- short CustFindNextPrev(short direction);
- short CustInsert(void);
- short CustEdit(void);
- short CustDelete(void);
-
-
-
- //this code is used repeatedly. Transfers information from the
- //dialogs to the "current record" and vice versa
-
- void GetFromCustDialog(void){
- //cust name id = 3
- //cust num id = 2
- //cust string id = 7
- GetDialogItem(gCustomerDialog, 3, &itemType, &item, &box);
- GetDialogItemText(item, itemString);
- ScratchShort = (short)itemString[0];
- if (ScratchShort>31)
- ScratchShort = 31;
- BlockMove(itemString,gCustomer.CustName, ScratchShort+1);
- GetDialogItem(gCustomerDialog, 2, &itemType, &item, &box);
- GetDialogItemText(item, itemString);
- StringToNum(itemString,&ScratchLong);
- gCustomer.CustNo = ScratchLong;
- GetDialogItem(gCustomerDialog, 7, &itemType, &item, &box);
- GetDialogItemText(item, itemString);
- ScratchShort = (short)itemString[0];
- if (ScratchShort>39)
- ScratchShort = 39;
- BlockMove(itemString,gCustString,ScratchShort + 1);
- }
-
- void PutToCustDialog(void){
- //cust name id = 3
- //cust num id = 2
- //Custstring id = 7
- GrafPtr oldPort;
- GetPort(&oldPort);
- SetPort((GrafPtr)gCustomerDialog);
- GetDialogItem(gCustomerDialog, 3, &itemType, &item, &box);
- ScratchShort = (short)(gCustomer.CustName[0]);
- if (ScratchShort>31)
- ScratchShort = 31;
- BlockMove(gCustomer.CustName,itemString, ScratchShort+1);
- SetDialogItemText(item, itemString);
- SetDialogItem(gCustomerDialog, 3, itemType, item, &box);
- InvalThisItemRect(gCustomerDialog,3);
- GetDialogItem(gCustomerDialog, 2, &itemType, &item, &box);
- ScratchLong = gCustomer.CustNo;
- NumToString(ScratchLong,itemString);
- SetDialogItemText(item, itemString);
- SetDialogItem(gCustomerDialog, 2, itemType, item, &box);
- InvalThisItemRect(gCustomerDialog,2);
- GetDialogItem(gCustomerDialog, 7, &itemType, &item, &box);
- ScratchShort = (short)(gCustString[0]);
- if (ScratchShort>39)
- ScratchShort = 39;
- BlockMove(gCustString,itemString, ScratchShort+1);
- SetDialogItemText(item, itemString);
- SetDialogItem(gCustomerDialog, 7, itemType, item, &box);
- InvalThisItemRect(gCustomerDialog,7);
- BeginUpdate(gCustomerDialog);
- DrawDialog(gCustomerDialog);
- EndUpdate(gCustomerDialog);
- SetPort(oldPort);
- }
- // generic error handler; simply puts up an alert if called.
- void HandleErrorMsg(short theErr){
- short AlertReturn;
- long ErrorLong;
- Str255 ErrorString;
- ErrorLong = theErr;
- NumToString(ErrorLong, ErrorString);
- ParamText(ErrorString, "\p", "\p", "\p");
- AlertReturn = StopAlert(129, NULL);
- }
-
- //another utility function
- short RetrieveString (void){
- short StringErr;
- StringErr = Read_Data(gInvoiceFCH,kCustStringSize,gCustomer.StringAddr,(Ptr)gCustString);
- }
-
- //these two functions could easily be made into one
- //do it in two for demonstrative purposes
- short FindCustByNumber(void){
- short FindErr= noErr;
- short StringSize;
- FileAddr foundAddr;
- // get data
- GetFromCustDialog();
- // set up search
- SearchNumP = gCustomer.CustNo;
- gCustomerFindType = kFindByNumber;
- // find on customer no. in Customer number tree
- FindErr = Find_Equal(kCustomerNumTree,gInvoiceFCH,(Ptr)&SearchNumP,(Ptr)&FoundNumP,&foundAddr,kLongCompare);
- if (FindErr != noErr) {
- HandleErrorMsg(FindErr);
- return FindErr;
- };
- // read the data in from the address that was passed to &foundAddr
- FindErr = Read_Data(gInvoiceFCH, kCustomerSize,foundAddr,(Ptr)&gCustomer);
- if (FindErr != noErr) {
- HandleErrorMsg(FindErr);
- return FindErr;
- };
- // pull in the child record
- FindErr = RetrieveString();
- if (FindErr != noErr) {
- HandleErrorMsg(FindErr);
- return FindErr;
- };
- // update the display
- PutToCustDialog();
- // set up the variables that will survive across function calls.
- SearchNumP = FoundNumP;
- CurrentCustomerAddr = foundAddr;
- return FindErr;
- }
-
- // see find by number above; this is very similar code.
- short FindCustByName(void){
- short FindErr= noErr;
- short StringSize;
- FileAddr foundAddr;
-
- GetFromCustDialog();
- StringSize = gCustomer.CustName[0];
- if (StringSize>31)
- StringSize = 31;
- BlockMove((Ptr)gCustomer.CustName,(Ptr)SearchNameP,StringSize+1);
- gCustomerFindType = kFindByName;
- FindErr = Find_Equal(kCustomerNameTree,gInvoiceFCH,(Ptr)SearchNameP,(Ptr)FoundNameP,&foundAddr,kPStringCompare);
- if (FindErr != noErr) {
- HandleErrorMsg(FindErr);
- return FindErr;
- };
- FindErr = Read_Data(gInvoiceFCH, kCustomerSize,foundAddr,(Ptr)&gCustomer);
- if (FindErr != noErr) {
- HandleErrorMsg(FindErr);
- return FindErr;
- };
- FindErr = RetrieveString();
- if (FindErr != noErr) {
- HandleErrorMsg(FindErr);
- return FindErr;
- };
- PutToCustDialog();
- BlockMove((Ptr)FoundNameP,(Ptr)SearchNameP, 256);;
- CurrentCustomerAddr = foundAddr;
- return FindErr;
- }
-
- short CustFindNextPrev(short direction){
- short FindErr = noErr;
- FileAddr foundAddr;
- FileAddr prevAddr;
- short TreeNo;
- Ptr SearchP;
- Ptr FoundP;
- CompareProcPtr Comparison;
-
- if (gCustomerFindType == kFindByNumber) {
- SearchP = (Ptr)&SearchNumP;
- FoundP = (Ptr)&FoundNumP;
- TreeNo = kCustomerNumTree;
- Comparison = kLongCompare;
- prevAddr = CurrentCustomerAddr;
- }
- else {
- SearchP = (Ptr)SearchNameP;
- FoundP = (Ptr)FoundNameP;
- TreeNo = kCustomerNameTree;
- Comparison = kPStringCompare;
- prevAddr = CurrentCustomerAddr;
- };
-
- if (direction == kNext)
- FindErr = Find_Next(TreeNo, gInvoiceFCH, SearchP, prevAddr, FoundP, &foundAddr, Comparison);
- else
- FindErr = Find_Previous(TreeNo, gInvoiceFCH, SearchP, prevAddr, FoundP, &foundAddr, Comparison);
- if (FindErr != noErr) {
- HandleErrorMsg(FindErr);
- return FindErr;
- };
- FindErr = Read_Data(gInvoiceFCH, kCustomerSize,foundAddr,(Ptr)&gCustomer);
- if (FindErr != noErr) {
- HandleErrorMsg(FindErr);
- return FindErr;
- };
- FindErr = RetrieveString();
- if (FindErr != noErr) {
- HandleErrorMsg(FindErr);
- return FindErr;
- };
- PutToCustDialog();
- if (gCustomerFindType == kFindByNumber) {
- CurrentCustomerAddr = foundAddr;
- SearchNumP = FoundNumP;
- }
- else {
- CurrentCustomerAddr = foundAddr;
- BlockMove((Ptr)FoundNameP, (Ptr)SearchNameP, 256);
- }
- return FindErr;
- }
-
- short CustInsert(void) {
- short InsertErr = noErr;
- FileAddr InsertAddr;
- short AlertReturn;
- short StringSize;
- //first,check and make sure that the proposed ID number is not in the tree
- GetFromCustDialog();
- SearchNumP = gCustomer.CustNo;
- InsertErr = Find_Equal(kCustomerNumTree,gInvoiceFCH,(Ptr)&SearchNumP,(Ptr)&FoundNumP,&InsertAddr,kLongCompare);
- if (SearchNumP == FoundNumP) {
- AlertReturn = CautionAlert(130, NULL);
- InsertErr = paramErr;
- return paramErr;
- };
- //get file space
- // ***must insert the child record first!!!
- InsertErr = Get_Bytes(gInvoiceFCH, &InsertAddr, kCustStringSize);
- if (InsertErr != noErr) {
- HandleErrorMsg(InsertErr);
- return InsertErr;
- };
- //write the child data
- InsertErr = Write_Data(gInvoiceFCH, kCustStringSize,InsertAddr, (Ptr)gCustString);
- if (InsertErr != noErr) {
- HandleErrorMsg(InsertErr);
- return InsertErr;
- };
- //put the child address into the parent record
- gCustomer.StringAddr = InsertAddr;
- //now insert the parent record
- InsertErr = Get_Bytes(gInvoiceFCH, &InsertAddr, kCustomerSize);
- if (InsertErr != noErr) {
- HandleErrorMsg(InsertErr);
- return InsertErr;
- };
- //write the record to file
- InsertErr = Write_Data(gInvoiceFCH, kCustomerSize, InsertAddr, (Ptr)&gCustomer);
- if (InsertErr != noErr) {
- HandleErrorMsg(InsertErr);
- return InsertErr;
- };
- //insert the keys
- InsertErr = Insert_Key(kCustomerNumTree, kUniqueKeys, gInvoiceFCH,(Ptr)&SearchNumP, InsertAddr, kLongCompare);
- if (InsertErr != noErr) {
- HandleErrorMsg(InsertErr);
- return InsertErr;
- // if for real, should also delete the record and release file space if failed insertion
- };
- StringSize = gCustomer.CustName[0];
- if (StringSize>31)
- StringSize = 31;
- BlockMove((Ptr)gCustomer.CustName,(Ptr)SearchNameP,StringSize+1);
- InsertErr = Insert_Key(kCustomerNameTree, kDuplicateKeys, gInvoiceFCH,(Ptr)&SearchNameP, InsertAddr, kPStringCompare);
- if (InsertErr != noErr) {
- HandleErrorMsg(InsertErr);
- return InsertErr;
- // if for real, should also delete the record and release file space if failed insertion
- };
- CurrentCustomerAddr = InsertAddr;
-
- return InsertErr;
- }
-
- short CustEdit(void) {
- short EditErr = noErr;
- short AlertReturn;
- FileAddr EditAddr;
- //the record in the "main buffer" should be moved to a secondary buffer
- //the child record **in this case** doesn't need to be protected
- CustomerType CustomerEdit;
-
- BlockMove(&gCustomer,&CustomerEdit,kCustomerSize);
- GetFromCustDialog();
- // to protect data integrity, the ID # cannot be edited
- // thus, only the strings need to be compared.
- // more complex approach would be needed in real app
- // if strings different, insert new record, edit the key string
- if (!EqualString(gCustomer.CustName, CustomerEdit.CustName, TRUE, FALSE)) {
- EditAddr = CurrentCustomerAddr;
- //overwrite the old data
- EditErr = Write_Data(gInvoiceFCH, kCustomerSize, EditAddr, (Ptr)&gCustomer);
- if (EditErr != noErr) {
- HandleErrorMsg(EditErr);
- return EditErr;
- };
- EditErr = Write_Data(gInvoiceFCH,kCustStringSize,CustomerEdit.StringAddr,(Ptr)gCustString);
- //Very Important. Delete the old key
- EditErr = Delete_Key(kCustomerNameTree,kDuplicateKeys, gInvoiceFCH, (Ptr)CustomerEdit.CustName, &EditAddr, kPStringCompare);
- if (EditErr != noErr) {
- HandleErrorMsg(EditErr);
- return EditErr;
- };
- //Insert the new Key
- EditErr = Insert_Key(kCustomerNameTree, kDuplicateKeys, gInvoiceFCH,(Ptr)gCustomer.CustName, EditAddr, kPStringCompare);
- if (EditErr != noErr) {
- HandleErrorMsg(EditErr);
- return EditErr;
- };
- };
- BlockMove((Ptr)gCustomer.CustName,(Ptr)SearchNameP,256);
- CurrentCustomerAddr = EditAddr;
- return EditErr;
- }
-
- short CustDelete(void) {
- short DeleteErr = noErr;
- short AlertReturn;
- FileAddr DeleteAddr;
- //assumes that a found, insert, edit done so there is a current record in buffer.
- DeleteAddr = CurrentCustomerAddr;
- //release keys first. better to have orphan record than keys pointing to nowhere
- DeleteErr = Delete_Key(kCustomerNameTree,kDuplicateKeys, gInvoiceFCH, (Ptr)gCustomer.CustName, &DeleteAddr, kPStringCompare);
- if (DeleteErr != noErr) {
- HandleErrorMsg(DeleteErr);
- return DeleteErr;
- };
- DeleteErr = Delete_Key(kCustomerNumTree,kUniqueKeys, gInvoiceFCH, (Ptr)gCustomer.CustNo, &DeleteAddr, kLongCompare);
- if (DeleteErr != noErr) {
- HandleErrorMsg(DeleteErr);
- return DeleteErr;
- };
- //release space in file
- //release child record first
- DeleteErr = Release_Bytes(gInvoiceFCH, gCustomer.StringAddr, kCustStringSize);
- if (DeleteErr != noErr) {
- HandleErrorMsg(DeleteErr);
- return DeleteErr;
- };
- // now release the customer rec itself
- DeleteErr = Release_Bytes(gInvoiceFCH, DeleteAddr, kCustomerSize);
- if (DeleteErr != noErr) {
- HandleErrorMsg(DeleteErr);
- return DeleteErr;
- };
- CurrentCustomerAddr = noAddr;
- gCustomer.CustName[0] = 0;
- gCustomer.CustNo = 0;
- PutToCustDialog();
- return DeleteErr;
- }
- // handles input from the modeless dialog
- void HandleCustomerAction(EventRecord *theEvent, short *theItem){
- short CustomerErr;
- ControlHandle popControl;
- short popValue;
- short itemNo = 2;
- short itemType;
- Handle item;
- Rect box;
- Boolean IsEditText = FALSE;
-
- switch (*theItem) {
- case kPopUp:
- popControl = GetCtlHandle(gCustomerDialog,kPopUp);
- popValue = GetControlValue(popControl);
-
- GetDialogItem(gCustomerDialog, itemNo, &itemType, &item, &box);
- if (itemType == editText)
- IsEditText = TRUE;
- switch (popValue) {
- case kFindByName:
- gCustomerAction = kFindByName;
- itemType = editText;
- break;
- case kFindByNumber:
- gCustomerAction = kFindByNumber;
- itemType = editText;
- break;
- case kNext:
- gCustomerAction = kNext;
- itemType = editText;
- break;
- case kPrevious:
- gCustomerAction = kPrevious;
- itemType = editText;
- break;
- case kInsert:
- gCustomerAction =kInsert;
- itemType = editText;
- break;
- case kEdit:
- gCustomerAction = kEdit;
- itemType = statText;
- break;
- case kDelete:
- gCustomerAction = kDelete;
- break;
- };//popup value switch
- if ((!IsEditText && (itemType == editText))||(IsEditText && (itemType == statText))) {
- // not gorgeous, but it works
- SetDialogItem(gCustomerDialog, itemNo, itemType, item, &box);
- InsetRect(&box, -5, -5);
- EraseRect(&box);
- InvalThisItemRect(gCustomerDialog,itemNo);
- SelectDialogItemText(gCustomerDialog, 3, 0, 0);
- BeginUpdate(gCustomerDialog);
- DrawDialog(gCustomerDialog);
- EndUpdate(gCustomerDialog);
- };
- break; //popup
-
- case kDoIt:
- switch (gCustomerAction) {
- case kFindByName:
- CustomerErr = FindCustByName();
- break;
- case kFindByNumber:
- CustomerErr = FindCustByNumber();
- break;
- case kNext:
- CustomerErr = CustFindNextPrev(kNext);
- break;
- case kPrevious:
- CustomerErr = CustFindNextPrev(kPrevious);
- break;
- case kInsert:
- CustomerErr =CustInsert();
- break;
- case kEdit:
- CustomerErr = CustEdit();
- break;
- case kDelete:
- CustomerErr = CustDelete();
- break;
- };//gCustomerAction switch
- break;
- }//switch
- }//function